<?php

use Biz\cache\wiki\CGoodsAlbum;
use Biz\model\wiki\MGoodsAlbum;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Biz\service\NiceGoodsTopic\Topic;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\ActionLog;
use Doraemon\tools\BlockType;
use Doraemon\tools\Hhzurl;
use Doraemon\tools\ImgUrl;

/**
 * 6.2版本之前 清单专辑
 * @deprecated
 */
class GoodsAlbumAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;
    const IS_LOG_EXPOSE = 1;


    /**
     * 获取详情页推荐wiki列表
     * @return array|array[]
     */
    public function main()
    {
        if (version_compare($this->appVersion, '6.1.0', ">=")) {
            $albumList = $this->getAlbumList();
            $topicList = $this->getTopic();
            $albumListUrl = Hhzurl::H5GoodsAlbumLit();
            $this->setSuccess([
                'album_list' => $albumList,
                'more_album_link' => \Biz\tools\helper\Common::formatFullScreenLink($albumListUrl),
                'topic' => $topicList,
            ]);
        } else {
            $uid = $this->uid ?: 1000;
            $id = CGoodsAlbum::getUserView($uid);
            $info = MGoodsAlbum::apiList($id, $view = in_array($uid, [2537919]));
            $list = [];
            $response = [
                //  'type' => BlockType::TYPE_GOODS_ALBUM,
                'title' => $info['title'] ?? '',
                'titleColor' => $info['color'] ?? '#6B1F13',
                'subtitle' => $info['subtitle'] ?? '',
                'subtitleColor' => $info['subtitle_color'] ?? '#C49995',
                'bgPic' => ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $info['pic'] ?? 'a6cda30tr0e60TL00r58841', ImgUrl::IMAGE_SIZE_OP_ORI),
                'bgIcon' => ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $info['icon'] ?? '192bf307f02l0xE00r5882t', ImgUrl::IMAGE_SIZE_OP_ORI),
                'list' => &$list,
            ];

            // 记录浏览index
            CGoodsAlbum::userView($uid, $info['index'] ?? 0);
            $id = $info['id'] ?? 0;
            $wids = $info['wiki'] ?? '';
            $wids = explode(',', $wids);
            shuffle($wids);
            $wids = array_slice($wids, 0, 15);
            $uid = $this->uid;
            $wiki = array_values(\Biz\service\wiki\Wiki::getWikiList($wids, $uid, \Biz\service\wiki\Wiki::FIELDS_GROUP_BASE_LIST));
            foreach ($wiki as $v) {
                $middleLink = \Doraemon\tools\Hhzurl::H5WikiMiddleList($v['id'], ActionLog::GOODTINGS_ALBUM) . "&goods_album=1&album_id=" . $id;
                $list[] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'middle_link' => \Biz\tools\helper\Common::formatFullScreenLink($middleLink),
                    'cover_img' => $v['cover_img'],
                    'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_FIND_WIKI_GOODS_ALBUM, [
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => 1,
                        basicStatSign::EXTEND_KEY_OBJ_ID => $v['id'],
                    ], ['brand_id' => $v['brand_id'], 'act_from' => 'goods_album', 'goods_album_id' => $id, 'wiki_id' => $v['id']]),
                ];
            }
//      若后台上传时数量小于等于15则有多少展示多少
//		若后台上传时数量大于15则随机展示15个商品
            $list = \Doraemon\tools\StatSign::versionCompatible($list, \Doraemon\tools\StatSign::BRAND_UP_GRADE_VERSION_521, BlockType::TYPE_NICE_GOODS_ALBUM, 'album_goods_info');
            ActionLog::log($this->uid, compact('uid', 'id'), ActionLog::GOODTINGS_ALBUM);

            $topic_list = Topic::getFirstTopicList();
            $topic_list = \Doraemon\tools\StatSign::versionCompatible($topic_list, \Doraemon\tools\StatSign::BRAND_UP_GRADE_VERSION_521, BlockType::TYPE_NICE_GOODS_TOPIC, 'topic_info');
            if ($topic_list && is_array($topic_list)) {
                foreach ($topic_list as &$val) {
                    $val['statSign'] = $val['topic_info']['second_topic_info']['statSign'];
                    unset($val['topic_info']['second_topic_info']['statSign']);
                }
            }
            $this->setSuccess(['album' => $response, 'topic' => $topic_list]);

        }

    }

    private function getAlbumList()
    {
        $result = [];
        $res = MGoodsAlbum::apiAllList($view = in_array($this, [2537919]), 1, 6);
        $total = $res['total'];
        $wikiIds = [];
        $list = [];
        foreach ($res['list'] as $row) {
            $wiki = $row['wiki'] ? explode(',', $row['wiki']) : [];
            $wikiId = $wiki ? $wiki[0] : 0;
            $row['wiki_id'] = $wikiId;
            $wikiIds[] = $wikiId;
            $list[] = $row;
        }
        $wikiList = $wikiIds ? clsWiki::getWikiBaseInfos($wikiIds) : [];
        $index = 1;
        foreach ($list as $item) {
            $wikiId = $item['wiki_id'];
            $coverImg = $wikiList[$wikiId]['cover_img'] ?? '';
            $middleLink = $wikiId ? Hhzurl::H5WikiMiddleList($wikiId, ActionLog::GOODTINGS_ALBUM) . "&goods_album=1&album_id=" . $item['id'] : '';
            if ($coverImg) {
                $result[] = [
                    'type' => BlockType::TYPE_GOODS_ALBUM,
                    'album_info' => [
                        'title' => $item['title'],
                        'cover_img' => $coverImg,
                        'middle_link' => \Biz\tools\helper\Common::formatFullScreenLink($middleLink),
                    ],
                    'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_TYPE_FIND_GOODS_PAGE_ALBUM_LIST, [
                        basicStatSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_GOODS_ALBUM,
                        basicStatSign::EXTEND_KEY_OBJ_ID => $item['id'],
                        basicStatSign::EXTEND_KEY_INDEX => $index
                    ]),

                ];
                $index++;
            } else {
                \Doraemon\tools\Log::debug('未查询到wiki到图片#wiki_id:' . $wikiId);
            }
        }
        return $result;
    }

    private function getTopic()
    {
        $result = [];
        $topicList = Topic::getFirstTopicList();
        if ($topicList && is_array($topicList)) {
            foreach ($topicList as $row) {
                $statSign = $row['second_topic_info']['statSign'];
                unset($row['second_topic_info']['statSign']);
                $result[] = [
                    'type' => BlockType::TYPE_NICE_GOODS_TOPIC,
                    'topic_info' => $row,
                    'statSign' => $statSign
                ];
            }
        }
        return $result;
    }
}